/**
 * @author : dormi330
 * @date : 2020-03-16
 * description : 描述 java8 Function<T,R> 的使用方式
 * Function.apply的使用方法
 */

package org.dormi.learn;

import java.util.Arrays;
import java.util.function.Function;

public class Employee {

    public String name;
    public int age;

    public Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public static void main(String[] args) {
        main1();
        main2();
    }

    public static void main1() {
        Function<Employee, String> fun1 = (Employee e) -> e.name + ":" + e.age;
        String desc = fun1.apply(new Employee("wzq", 33));
        System.out.println(desc);
    }

    public static void main2() {
        Function<Employee, String> fun1 = (Employee e) -> e.name + ":" + e.age;
        Function<Employee, String> fun2 = new Function<Employee, String>() {
            @Override
            public String apply(Employee e) {
                return e.name + ":" + e.age;
            }
        };

        Employee e1 = new Employee("wzq", 33);
        Arrays.asList(e1)
            .stream()
            // <R> Stream<R> map(Function<? super T, ? extends R> mapper);
            // 这里使用 lambda表达式(本质是匿名函数)
            .map(e -> e.name + ":" + e.age)
//            .map(fun1)
//            .map(fun2)
            .forEach(System.out::println);
    }
}
